package cc.rengu.igas.cops.common.util;

import cc.rengu.igas.cops.common.dao.CopsMchntBaseInfoMapper;
import cc.rengu.igas.cops.common.dao.CopsPlatMchntInfoMapper;
import cc.rengu.igas.cops.common.dao.impl.CopsMchntBaseInfoMapperImpl;
import cc.rengu.igas.cops.common.dao.impl.CopsPlatMchntInfoMapperImpl;
import cc.rengu.igas.cops.common.entity.CopsMchntBaseInfo;
import cc.rengu.igas.cops.common.entity.CopsPlatMchntInfo;
import cc.rengu.igas.cops.common.enums.RespCodeEnum;
import cc.rengu.igas.cops.common.enums.SignTypeEnum;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * @author panky
 * @version 1.0.0 创建时间：2020/08/05
 */
public class GrandGredeTool {

    protected RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /**
     * 获得用户授权签约信息
     *
     * @param signLevel
     * @param signAcct
     * @param signOrgn
     * @param mchntNo
     * @param mobile
     * @throws Exception
     */
    public GrandGredeInfoResponse grandGredeInfo(String signLevel, String signAcct, String signOrgn, String mchntNo, String mobile) throws Exception {
        String userId = null;
        String signInst = null;
        String signType = SignTypeEnum.BANK_CLASS_SIGN.getSignTypeCode();//默认行级签约
        //判断数据格式问题
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);

        if (StringUtil.isEmptyOrNull(signLevel)) {
            if (!StringUtil.isEmptyOrNull(mchntNo)) {
                //上传商户号，默认为商户级签约
                signType = SignTypeEnum.MCHNT_CLASS_SIGN.getSignTypeCode();
                signInst = mchntNo;
                userId = mobile.trim();
            } else {
                //未上传商户号，默认为行级签约
                signType = SignTypeEnum.BANK_CLASS_SIGN.getSignTypeCode();
                signInst = instId;
            }
        } else {
            switch (signLevel) {
                //银行
                case "0":
                    signType = SignTypeEnum.BANK_CLASS_SIGN.getSignTypeCode();
                    signInst = instId;
                    break;
                //商户
                case "1":
                    signType = SignTypeEnum.MCHNT_CLASS_SIGN.getSignTypeCode();
                    signInst = mchntNo;
                    userId = mobile.trim();
                    if (StringUtil.isEmptyOrNull(signInst)) {
                        rglog.info("商户级签约，商户号为空， inst_id:<{}>", instId);
                        throw new BizException(RespCodeEnum.MCHNTNO_NOT_BLANK.getRespCode(), RespCodeEnum.MCHNTNO_NOT_BLANK.getRespDesc());
                    }
                    break;
                //机构
                case "2":
                    signType = SignTypeEnum.PLAT_CLASS_SIGN.getSignTypeCode();
                    signInst = signOrgn;
                    userId = signAcct.trim();
                    if (StringUtil.isEmptyOrNull(signInst)) {
                        rglog.info("机构级签约，平台商户号为空， inst_id:<{}>", instId);
                        throw new BizException(RespCodeEnum.MCHNTNO_NOT_BLANK.getRespCode(), RespCodeEnum.MCHNTNO_NOT_BLANK.getRespDesc());
                    }
                    break;
                default:
                    rglog.info("不支持当前签约方式，inst_id:<{}>, signLevel:<{}>", instId, signLevel);
                    throw new BizException(RespCodeEnum.UNSUPPORT_SIGN_TYPE.getRespCode(), RespCodeEnum.UNSUPPORT_SIGN_TYPE.getRespDesc());
            }
        }

        //签约等级非行级时，signInst为商户号，校验商户信息，并根据商户签约设置重新设置签约等级和签约主体
        if (!signType.equals(SignTypeEnum.BANK_CLASS_SIGN.getSignTypeCode())) {

            CopsMchntBaseInfoMapper opmsMchntBaseInfoMapper = new CopsMchntBaseInfoMapperImpl();
            CopsPlatMchntInfoMapper opmsPlatMchntInfoMapper = new CopsPlatMchntInfoMapperImpl();
            CopsMchntBaseInfo mchntBaseInfo = opmsMchntBaseInfoMapper.getMchntBaseInfoByInstIdAndMchntNo(instId, signInst);
            CopsPlatMchntInfo platMchntInfo = null;

            /*
             * 1.判断商户是否是基础商户，若是基础商户且无平台商户则认定为非授信商户，若存在平台商户则查询所属的平台商户并判断是否授信
             * 2.商户为平台商户则直接判断商户是否是授信商户
             * 3.签约类型取商户的签约类型 分为：商户-平台-机构
             * 4.签约主体根据商户的签约类型取对应的值
             * 5.商户所属主体 基础商户取商户签约机构 平台商户取商户管理机构
             */
            if (null == mchntBaseInfo) {

                platMchntInfo = opmsPlatMchntInfoMapper.getOpmsPlatMchntInfoByInstIdMchntNo(instId, mchntNo);
                if (platMchntInfo == null) {
                    throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
                }
                signType = platMchntInfo.getSignType();
                //平台级签约
                if (SignTypeEnum.PLAT_CLASS_SIGN.getSignTypeCode().equals(signType)) {
                    signInst = platMchntInfo.getPlatMchntNo();
                }
            } else {

                if (!StringUtil.isEmptyOrNull(mchntBaseInfo.getPlatMchntNo())) {
                    platMchntInfo = opmsPlatMchntInfoMapper.getOpmsPlatMchntInfoByInstIdMchntNo(instId, mchntBaseInfo.getPlatMchntNo());
                }

                signType = mchntBaseInfo.getMchntSignType();
                if (SignTypeEnum.MCHNT_CLASS_SIGN.getSignTypeCode().equals(signType)) {
                    rglog.info("商户:<{}>,为商户级别签约", mchntNo);
                    throw new BizException(RespCodeEnum.UNSUPPORT_SIGN_TYPE.getRespCode(), RespCodeEnum.UNSUPPORT_SIGN_TYPE.getRespDesc());
                } else if (SignTypeEnum.PLAT_CLASS_SIGN.getSignTypeCode().equals(signType)) {
                    rglog.info("商户:<{}>,为平台级别签约", mchntNo);
                    signInst = mchntBaseInfo.getPlatMchntNo();
                }
            }

            // 平台级行级
            if (platMchntInfo != null
                    && SignTypeEnum.BANK_CLASS_SIGN.getSignTypeCode().equals(platMchntInfo.getSignType())) {
                rglog.info("商户:<{}>,为平台级行级", mchntNo);
                signInst = instId;
            }
        }

        GrandGredeInfoResponse response = new GrandGredeInfoResponse();
        response.setSignInst(signInst);
        response.setSignType(signType);
        response.setUserId(userId);
        return response;
    }

}
